//
//  TransitionViewController.m
//  OCAnimation
//
//  Created by zhouLiHuan on 2017/2/17.
//  Copyright © 2017年 zhouLiHuan. All rights reserved.
//

#import "TransitionViewController.h"

@interface TransitionViewController ()<CAAnimationDelegate>

@property (nonatomic,strong)UILabel *transitionLabel;
@property (nonatomic,assign)NSInteger index;
@property (nonatomic,strong)NSArray *animArr;


@end

@implementation TransitionViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.animArr = @[@"cube",@"suckEffect",@"rippleEffect",@"pageCurl",@"pageUnCurl",@"oglFlip",@"cameraIrisHollowOpen",@"cameraIrisHollowClose",@"spewEffect",@"genieEffect",@"unGenieEffect",@"twist",@"tubey",@"swirl",@"charminUltra",@"zoomyIn",@"zoomyOut",@"oglAppLicationSuspend"];
    self.view.backgroundColor = [UIColor whiteColor];
    self.transitionLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, 300)];
    self.transitionLabel.center = self.view.center;
    self.transitionLabel.backgroundColor = [UIColor redColor];
    self.transitionLabel.font = [UIFont systemFontOfSize:20];
    self.transitionLabel.numberOfLines = 0;
    self.transitionLabel.textColor = [UIColor blueColor];
    self.transitionLabel.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:self.transitionLabel];
    self.index = 0;
  
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.backgroundColor = [UIColor blueColor];
    btn.frame = CGRectMake(0, 0, 50, 40);
    btn.center = CGPointMake(self.view.width * 0.5, 640);
    [btn setTitle:@"动画" forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(btnAction) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn];
    
}

- (void)btnAction{
    CATransition *pageCurlAnim = [[CATransition alloc] init]; //转场动画
    pageCurlAnim.delegate = self;
    pageCurlAnim.type = self.animArr[self.index];
    pageCurlAnim.subtype = kCATransitionFromLeft;
    pageCurlAnim.repeatCount = 1;
    [pageCurlAnim setValue:@"transitionAnim" forKey:@"anim"];
    [self.transitionLabel.layer addAnimation:pageCurlAnim forKey:@"transition"];
    
}

- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag{
    self.index = (self.index < self.animArr.count - 1) ? self.index + 1 :0;
    if (flag) {
        CATransition *pageCurlAnim = [[CATransition alloc] init];
        pageCurlAnim.delegate = self;
        pageCurlAnim.duration = 1.0;
        pageCurlAnim.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];
        pageCurlAnim.type = self.animArr[self.index];
        pageCurlAnim.subtype = kCATransitionFromLeft;
        pageCurlAnim.autoreverses = YES;
        pageCurlAnim.repeatCount = 1;
        [pageCurlAnim setValue:@"transitionAnim" forKey:@"anim"];
        [self.transitionLabel.layer addAnimation:pageCurlAnim forKey:@"transtion"];
        self.transitionLabel.text = @"动画效果:\(self.animArr[self.index])";
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
